package com.jw.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.system.entity.ConGoods;
import com.jw.system.entity.Goods;
import com.jw.system.entity.User;
import com.jw.system.mapper.ConGoodsMapper;
import com.jw.system.mapper.GoodsMapper;
import com.jw.system.mapper.UserMapper;
import com.jw.system.service.GoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author jw
 * @since 2021-04-20
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ConGoodsMapper conGoodsMapper;

    @Override
    public Page<Goods> getGoodsList(Integer current, Integer size, Goods goods) {
        Page<Goods> page = new Page<>(current, size);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        if(goods.getOneCatId()!=null){
            queryWrapper.eq("one_cat_id",goods.getOneCatId());
        }
        if(goods.getTwoCatId()!=null){
            queryWrapper.eq("two_cat_id",goods.getTwoCatId());
        }
        if(goods.getName()!=null){
            queryWrapper.like("name",goods.getName());
        }
        Page<Goods> goodsPage = goodsMapper.selectPage(page, queryWrapper);
//        List<Goods> goodsList = goodsPage.getRecords();
//        for(Goods goods1:goodsList){
//            Integer price = 0;
//            QueryWrapper<ConGoods> conGoodsQueryWrapper = new QueryWrapper<>();
//            conGoodsQueryWrapper.eq("goods_id",goods1.getId());
//            List<ConGoods> conGoods = conGoodsMapper.selectList(conGoodsQueryWrapper);
//            for(ConGoods conGoods1:conGoods){
//                price = price + conGoods1.getPrice()*conGoods1.getOrderNum();
//            }
//            goods1.setPrice(price);
//        }
        return goodsPage;
    }

}
